package com.party.core.service.mall.biz;

import com.party.common.utils.DateUtils;
import com.party.common.utils.StringUtils;
import com.party.core.model.mall.MallDataCount;
import com.party.core.model.mall.MallDataDetail;
import com.party.core.service.mall.IMallDataCountService;
import com.party.core.service.mall.IMallDataDetailService;
import com.party.core.service.mall.IMallOrderDetailService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description: 商品数据业务统计
 * @Author: yangshoukun
 * @Date: 2018/5/23 15:59
 */
@Service
public class MallDataBizService {

    @Autowired
    private IMallDataCountService mallDataCountService;

    @Autowired
    private IMallDataDetailService mallDataDetailService;

    @Autowired
    private IMallOrderDetailService mallOrderDetailService;

    /**
     * 商城数据统计
     * @param input
     */
    private MallDataCount calcCount(MallDataCount input, boolean isAdd) {
        MallDataCount mallDataCount = null;
        if (StringUtils.isNotEmpty(input.getId())) {
            mallDataCount = mallDataCountService.get(input.getId());
        } else if (StringUtils.isNotEmpty(input.getMemberId())) {
            mallDataCount = mallDataCountService.findByMemberId(input.getMemberId());
        }
        if (null == mallDataCount) {
            mallDataCount = new MallDataCount();
            if (StringUtils.isNotEmpty(input.getMemberId())) {
                mallDataCount.setMemberId(input.getMemberId());
            }
            mallDataCount.setCreateBy(input.getCreateBy());
            mallDataCount.setUpdateBy(input.getUpdateBy());
        }
        calcMallDataCount(input, mallDataCount, isAdd);
        if (StringUtils.isNotEmpty(mallDataCount.getId())) {
            mallDataCountService.update(mallDataCount);
        } else {
            mallDataCountService.insert(mallDataCount);
        }
        return mallDataCount;
    }

    private void calcMallDataCount(MallDataCount from, MallDataCount to, boolean isAdd) {
        if (null != from.getIncome()) {
            to.setIncome(isAdd ? to.getIncome().add(from.getIncome()) : to.getIncome().subtract(from.getIncome()));
        }
        if (null != from.getOrderNum()) {
            to.setOrderNum(isAdd ? to.getOrderNum() + from.getOrderNum() : to.getOrderNum() - from.getOrderNum());
        }
        if (null != from.getSalesNum()) {
            to.setSalesNum(isAdd ? to.getSalesNum() + from.getSalesNum() : to.getSalesNum() - from.getSalesNum());
        }
        if (null != from.getTurnover()) {
            to.setTurnover(isAdd ? to.getTurnover().add(from.getTurnover()) : to.getTurnover().subtract(from.getTurnover()));
        }
    }

    private void calcMallDataDetail(MallDataCount from, MallDataDetail to, boolean isAdd) {
        if (null != from.getIncome()) {
            to.setIncome(isAdd ? to.getIncome().add(from.getIncome()) : to.getIncome().subtract(from.getIncome()));
        }
        if (null != from.getOrderNum()) {
            to.setOrderNum(isAdd ? to.getOrderNum() + from.getOrderNum() : to.getOrderNum() - from.getOrderNum());
        }
        if (null != from.getSalesNum()) {
            to.setSalesNum(isAdd ? to.getSalesNum() + from.getSalesNum() : to.getSalesNum() - from.getSalesNum());
        }
        if (null != from.getTurnover()) {
            to.setTurnover(isAdd ? to.getTurnover().add(from.getTurnover()) : to.getTurnover().subtract(from.getTurnover()));
        }
    }

    /**
     * 商城数据统计详情
     * @param input
     * @param isAdd
     */
    @Transactional
    public void calcDataCount(MallDataCount input, boolean isAdd) {
        MallDataCount mallDataCount = calcCount(input, isAdd);

        MallDataDetail param = new MallDataDetail();
        param.setCountId(mallDataCount.getId());
        param.setCreateDate(new Date());
        List<MallDataDetail> list = mallDataDetailService.listPage(param, null);

        MallDataDetail mallDataDetail;
        if (CollectionUtils.isNotEmpty(list)) {
            mallDataDetail = list.get(0);
        } else {
            mallDataDetail = new MallDataDetail();
            mallDataDetail.setMemberId(mallDataCount.getMemberId());
            mallDataDetail.setCountId(mallDataCount.getId());
            mallDataDetail.setCreateBy(mallDataCount.getCreateBy());
            mallDataDetail.setUpdateBy(mallDataCount.getUpdateBy());
        }

        calcMallDataDetail(input, mallDataDetail, isAdd);
        if (StringUtils.isNotEmpty(mallDataDetail.getId())) {
            mallDataDetailService.update(mallDataDetail);
        } else {
            mallDataDetailService.insert(mallDataDetail);
        }
    }

    /**
     * 矫正统计表
     */
    public void initDataCount() {
        initFunction(null);
    }

    private void initFunction(String memberId) {
        MallDataCount param = new MallDataCount();
        param.setMemberId(memberId);
        param.setIncome(null);
        param.setSalesNum(null);
        param.setTurnover(null);
        param.setOrderNum(null);

        List<MallDataCount> mallDataCounts = mallDataCountService.listPage(param, null);
        mallDataCounts.forEach(dataCount -> {
            if (StringUtils.isEmpty(dataCount.getMemberId())) {
                return;
            }
            Map<String, Object> data = mallOrderDetailService.findDataCountByDistributor(dataCount.getMemberId(), null);
            if (null == data) {
                return;
            }
            // 订单量
            Integer orderNum = data.get("orderNum") == null ? 0 : Integer.parseInt(data.get("orderNum").toString());
            dataCount.setOrderNum(orderNum);
            // 成交金额
            BigDecimal turnover = data.get("turnover") == null ? new BigDecimal(0) : (BigDecimal) data.get("turnover");
            dataCount.setTurnover(turnover);
            // 销售量
            Integer salesNum = data.get("salesNum") == null ? 0 : Integer.parseInt(data.get("salesNum").toString());
            dataCount.setSalesNum(salesNum);
            // 机构收益
            BigDecimal income = data.get("income") == null ? new BigDecimal(0) : (BigDecimal) data.get("income");
            dataCount.setIncome(income);

            mallDataCountService.update(dataCount);
        });
    }

    /**
     * 矫正昨天的数据统计详情
     */
    public void reviseYesterday() {
        Date yesterday = DateUtils.addDay(new Date(), -1);
        reviseSomeDay(yesterday);
    }

    /**
     * 矫正今天的数据统计详情
     */
    public void reviseToday() {
        Date date = new Date();
        reviseSomeDay(date);
    }

    /**
     * 矫正某一天的数据统计详情表
     * @param date
     */
    private void reviseSomeDay(Date date) {
        reviseFunction(date, null);
    }

    private void reviseFunction(Date date, String memberId) {
        if (DateUtils.diffDate(new Date(), date) < 0) {
            return;
        }
        MallDataDetail param = new MallDataDetail();
        param.setIncome(null);
        param.setSalesNum(null);
        param.setTurnover(null);
        param.setOrderNum(null);
        param.setCreateDate(date);
        param.setMemberId(memberId);
        List<MallDataDetail> list = mallDataDetailService.listPage(param, null);
        list.forEach(dataDetail -> {
            Map<String, Object> data = mallOrderDetailService.findDataCountByDistributor(dataDetail.getMemberId(), date);
            if (null == data) {
                return;
            }
            // 订单量
            Integer orderNum = data.get("orderNum") == null ? 0 : Integer.parseInt(data.get("orderNum").toString());
            dataDetail.setOrderNum(orderNum);
            // 成交金额
            BigDecimal turnover = data.get("turnover") == null ? new BigDecimal(0) : (BigDecimal) data.get("turnover");
            dataDetail.setTurnover(turnover);
            // 销售量
            Integer salesNum = data.get("salesNum") == null ? 0 : Integer.parseInt(data.get("salesNum").toString()) ;
            dataDetail.setSalesNum(salesNum);
            // 机构收益
            BigDecimal income = data.get("income") == null ? new BigDecimal(0) : (BigDecimal) data.get("income");
            dataDetail.setIncome(income);

            mallDataDetailService.update(dataDetail);
        });
    }

    /**
     * 立即矫正
     * @param memberId
     */
    public void reviseNow(String memberId) {
        initFunction(memberId);
        reviseFunction(new Date(), memberId);
    }
}
